package com.zd.commodity.productMng.dao.impl;

import javax.annotation.Resource;

import org.hibernate.SessionFactory;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.zd.commodity.dao.impl.BaseDao;
import com.zd.commodity.productMng.dao.IAttrRuleDao;
import com.zd.commodity.productMng.entity.AttrRule;


@Repository("attrRuleDao")
public class AttrRuleDaoImpl extends BaseDao<AttrRule> implements IAttrRuleDao {

	@Resource(name = "sessionFactory")
	public void setSuperSessionFactory(SessionFactory sessionFactory){
		super.setSessionFactory(sessionFactory);
	}
	
	@Override
	public DetachedCriteria getDetachedCriteria(AttrRule t1,AttrRule t2,Object params){
		DetachedCriteria dc=DetachedCriteria.forClass(AttrRule.class);
		if(t1!=null){
			if(t1.getRegex()!=null && !t1.getRegex().equals("")){
				dc.add(Restrictions.like("regex", t1.getRegex(), MatchMode.ANYWHERE));
			}
			
			if(t1.getCategory()!=null){
				dc.createAlias("category", "c");
				if(t1.getCategory().getName()!=null && !t1.getCategory().getName().equals("")){
					dc.add(Restrictions.like("c.name", t1.getCategory().getName(),MatchMode.ANYWHERE));
				}
			}
			if(t1.getBrandShow()!=null){
				dc.createAlias("brandShow", "bs");
				if(t1.getBrandShow().getName()!=null && !t1.getBrandShow().getName().equals("")){
					dc.add(Restrictions.like("bs.name", t1.getBrandShow().getName(),MatchMode.ANYWHERE));
				}
			}
			if(t1.getProductAttr()!=null){
				dc.createAlias("productAttr", "pa");
				if(t1.getProductAttr().getName()!=null && !t1.getProductAttr().getName().equals("")){
					dc.add(Restrictions.like("pa.name", t1.getProductAttr().getName(),MatchMode.ANYWHERE));
				}
			}
		}
		return dc;
	}
}
